package dealfunc

import (
	"net/http"
	"os"
	"github.com/gin-gonic/gin"
)

func writeComment(account string, comment string) (error) {
	file, err := os.OpenFile("commentData.txt", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)
	if err != nil {
		return err
	}
	defer file.Close()

	file.WriteString("\n" + account + "\n")
	file.WriteString(comment + "\n" + "!@#$%^&*()_+")

	return nil
}

func AddComment(c *gin.Context) {
	// 评论信息
	var commentData map[string]string

	if err := c.BindJSON(&commentData); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"result": "false",
			"message": "read wrong",
		})
		return
	}

	account := commentData["account"]
	comment := commentData["comment"]

	err := writeComment(account, comment)

	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"result": "false",
			"message": "write wrong",
		})
		return
	}

	// 成功
	c.JSON(http.StatusAccepted, gin.H{
		"result": "true",
		"message": "成功",
	})
}